#!/bin/sh
#
# This script is a wrapper for MPICH mpirun
# it generates the machine file based on the hosts
# given to it by Quickpool.
#

MPIRUN=`which mpirun`

if [ x"${QP_JOBFILENAME}" = x -o x"${QP_HOSTS}" = x ]; then
    ${MPIRUN} $*
    exit $?
fi
    
MYARGS=$*
WORKDIR=`dirname ${QP_JOBFILENAME}`
MACHFILE=${WORKDIR}/mpi_machines.${QP_JOBID}

T=`echo "${MYARGS}" | grep -- -machinefile |wc -l`
if [ $T -gt 0 ]; then
    echo "Error:  Do not provide the machinefile for mpirun."
    echo "        It is generated automatically for you."
    exit -3
fi

# Make the MPICH machine file
echo "${QP_HOSTS}" | tr '\/ ' '\r\n' > ${MACHFILE}

${MPIRUN} -machinefile ${MACHFILE} -launcher-exec qptask ${MYARGS}
ret=$?

rm -rf ${MACHFILE}

exit $ret
